﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using RibbonEditor.Rules;

namespace RibbonEditor.Controls.Rules
{
    public partial class RuleControl : UserControl, IControlRulable
    {
        IRule rule;

        internal IRule Rule
        {
            get { return this.rule; }
            set { this.rule = value; }
        }

        public RuleControl(IRule iRule)
        {
            InitializeComponent();
            this.rule = iRule;
            
            if (rule is EnableRule)
            {
                if (((EnableRule)rule).IsParentNode)
                {
                    this.rcId.Visible = false;
                    this.rcId.IsRequired = false;
                }
                else
                {
                    this.rcId.Location = this.stringControl1.Location;
                    this.rcId.Prefix = "EnableRule";
                    this.stringControl1.Visible = false;
                    this.stringControl1.IsRequired = false;
                }
            }
            else if (rule is DisplayRule)
            {
                if (((DisplayRule)rule).IsParentNode)
                {
                    this.rcId.Visible = false;
                    this.rcId.IsRequired = false;
                }
                else
                {
                    this.rcId.Location = this.stringControl1.Location;
                    this.rcId.Prefix = "DisplayRule";
                    this.stringControl1.Visible = false;
                    this.stringControl1.IsRequired = false;
                }
            }

            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                if (this.stringControl1.Visible)
                {
                    this.stringControl1.Value = this.rule.GetId();
                }

                if (this.rcId.Visible)
                {
                    this.rcId.Value = this.rule.GetId();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (this.stringControl1.Value.Length > 0)
                {
                    this.rule.SetId(this.stringControl1.Value);
                }
                else if (this.rcId.Value.Length > 0)
                {
                    this.rule.SetId(this.rcId.Value);
                }

                UserControlHelper.CleanDirtyFields(this);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildObjectXml(XmlNode parentNode)
        {
            try
            {
                if (this.rule is IEnableRule)
                {
                    ((IEnableRule)this.rule).AddNode(parentNode);
                }
                else if (this.rule is IDisplayRule)
                {
                    ((IDisplayRule)this.rule).AddNode(parentNode);
                }
                else
                {
                    ((ITabDisplayRule)this.rule).AddNode(parentNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
